﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace FooFun.Web.Utils
{
    public class SiteUrlsProvider
    {
        public static string GetGuidUrl(Guid guid)
        {
            string str = GetGuidBase64(guid);

            return HttpUtility.UrlEncode(str);
        }

        public static Guid GetGuidFromUrl(string str)
        {
            Guid guid = Guid.Empty;
            try
            {
                str = HttpUtility.UrlDecode(str);
                guid = GetGuid(str);
            }
            catch (Exception)
            {
            }

            return guid;
        }

        public static string GetGuidBase64(Guid obj)
        {
            Guid guid = (Guid)obj;
            string guidStr = Convert.ToBase64String(
                guid.ToByteArray());

            return guidStr;
        }

        public static Guid GetGuid(string str)
        {
            if (str.Length == 24)
            {
                byte[] bytes = Convert.FromBase64String(str);

                Guid guid = new Guid(bytes);

                return guid;
            }
            else if (str.Length == 32)
            {
                Guid guid = new Guid(str);

                return guid;
            }
            else if (str.Length == 36)
            {
                Guid guid = new Guid(str);

                return guid;
            }
            return Guid.Empty;
        }
    }
}
